//
//  commentVC.m
//  HappyStore
//
//  Created by 陈锋 on 16/5/19.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "commentVC.h"

@interface commentVC ()<UITextViewDelegate>

@property (weak, nonatomic) IBOutlet UIButton *finishBtn;
@property (strong, nonatomic) UITextView *textView;
@property (strong ,nonatomic)UILabel *tip;
@property (strong, nonatomic)UILabel *count;

@end

@implementation commentVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"发表评论";
    
    self.finishBtn.layer.masksToBounds = YES;
    self.finishBtn.layer.cornerRadius = 10.0f;
    
    /*创建投诉文本框*/
    UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 66, [UIScreen mainScreen].bounds.size.width - 20, 300 - 20)];
    textView.text = @"";
    textView.font = [UIFont fontWithName:@"Arial" size:12.0f];
    textView.backgroundColor = [UIColor whiteColor];
    textView.textAlignment = NSTextAlignmentLeft;
    textView.autocapitalizationType = UITextAutocapitalizationTypeNone;
    textView.returnKeyType = UIReturnKeyDefault;
    textView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    textView.layer.borderWidth = 1.0;
    textView.layer.cornerRadius = 8.0f;
    textView.layer.masksToBounds = YES;
    textView.scrollEnabled = YES;
    textView.delegate = self;

    /*自定义文本框placeholder*/
    _tip = [[UILabel alloc]initWithFrame:CGRectMake(13, 69, self.view.width, 25)];
    _tip.text = @"输入你对项目的评论：";
    _tip.font = [UIFont fontWithName:@"Arial" size:12.0f];
    _tip.backgroundColor = [UIColor clearColor];
    _tip.enabled = NO;
    /*自定义文本框字数统计*/
    _count = [[UILabel alloc]initWithFrame:CGRectMake(textView.width - 60, textView.height + 30, 60, 30)];
    _count.text = @"0/200";
    _count.textAlignment = NSTextAlignmentRight;
    _count.font = [UIFont fontWithName:@"Arial" size:12.0f];
    _count.backgroundColor = [UIColor clearColor];
    _count.enabled = NO;
    
    self.textView = textView;
    [self.view addSubview:textView];
    [self.view addSubview:_tip];
    [self.view addSubview:_count];
    
    //设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"Back" highImage:@"Back"];
    self.navigationItem.leftBarButtonItem = leftItem;
}

-(void)leftBarButtonItemClick{
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)finishBtn:(UIButton *)sender {
    
    if (self.textView.text.length < 1) {
        
        [SVProgressHUD showWithStatus:@"内容不能为空!"];
        [SVProgressHUD dismissafterDelay:kHideHudDelay];
        
    }else{
        
        [self sendRequest];
        
    }
    
}

-(void)sendRequest{
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    dic[@"userId"] = allConstMemberid;
    dic[@"token"] = token;
    dic[@"resId"] = self.resId;

    dic[@"content"] = self.textView.text;
    
    [LSHttpTool sendHttpData:@"commentPppProject_app_ppp.im.lensum" params:dic success:^(id responseObject) {
        NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
        
        [SVProgressHUD showSuccessWithStatus:dicInfo[@"msg"]];
        [SVProgressHUD dismissafterDelay:kHideHudDelay];
        
        [self.navigationController popViewControllerAnimated:YES];
        
    } failure:^(NSError *error) {
        
        [SVProgressHUD showErrorWithStatus:@"网络连接错误"];
        [SVProgressHUD dismissafterDelay:kHideHudDelay];
        
    }];
}
#pragma mark - textView

// 自定义文本框placeholder

- (void)textViewDidChange:(UITextView *)textView {
    NSInteger number = [_textView.text length];
    if (number >= 300) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"字符个数不能大于300" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        _textView.text = [_textView.text substringToIndex:300];
        number = 300;
        
    }
    self.count.text = [NSString stringWithFormat:@"%ld/300",(long)number];
    
    if (self.textView.text.length > 0) {
        _tip.text = @"";
        
    }else{
        _tip.text = @"输入你对项目的评论";
    }
}
@end
